﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;

namespace SlaktDataReader
{
    public class BirthBookTextFile : TextFileBase
    {

        public List<BirthBookRecord> Records { get; set; }

        private BirthBook bk;

        public BirthBookTextFile()
        {
            _fields.Add("Lopnr", -1);
            _fields.Add("Sidnr", -1);
            _fields.Add("Ordnr", -1);
            _fields.Add("Fdatum", -1);
            _fields.Add("Dpdatum", -1);
            _fields.Add("Kon", -1);
            _fields.Add("Fnamn", -1);
            _fields.Add("Enamn", -1);
            _fields.Add("Adress", -1);
            _fields.Add("Nadress", -1);
            _fields.Add("Mtitel", -1);
            _fields.Add("Mfnamn", -1);
            _fields.Add("Menamn", -1);
            _fields.Add("Ktitel", -1);
            _fields.Add("Kfnamn", -1);
            _fields.Add("Kenamn", -1);
            _fields.Add("Kalder", -1);
            _fields.Add("Ovr1", -1);
            _fields.Add("Ovr2", -1);
            _fields.Add("Ovr3", -1);
            _fields.Add("Uppdaterat", -1);

            Separator = '\t';
            Records = new List<BirthBookRecord>();
            FileEncoding = Encoding.UTF7;
        }

        //public override int Read()
        //{
        //    int i, _LinesRead = 0;
        //    bool HeaderFound = false;
        //    string[] lines = {};
        //    try
        //    {
        //        if (FileName == null)
        //            return 0;

        //        if (FileName.ToUpper().StartsWith("HTTP://") || FileName.ToUpper().StartsWith("HTTPS://") || FileName.ToUpper().StartsWith("FTP://"))
        //        {
        //            lines = DownloadBook();
        //            if (lines == null)
        //                return 0;
        //        }
        //        else
        //        {
        //            lines = File.ReadAllLines(FileName, FileEncoding);
        //        }
        //        if (lines.Length == 0)
        //        {
        //            return 0;
        //        }

        //        for (i = 0; i < lines.Length; i++)
        //        {
        //            if (!HeaderFound)
        //            {
        //                if (lines[i].ToUpper().Contains("LOPNR"))
        //                {
        //                    HeaderFound = _ParseHeader(lines[i]);
        //                    continue;
        //                }
        //                else
        //                {
        //                    _ParseHeaderInfoLine(lines[i]);
        //                    continue;
        //                }
        //            }
        //            else
        //            {
        //                _LinesRead += _ParseLine(lines[i]);
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //        return -1;
        //    }
        //    return _LinesRead;
        //}



        protected override void _AddRecord(string[] RowFields)
        {
            if (RowFields.Length > 0)
            {
                BirthBookRecord r = new BirthBookRecord();
                for (int i = 0; i < RowFields.Length; i++)
                {
                    if (_fields.ContainsValue(i))
                    {
                        try
                        {
                            switch (_fields.Where(f => f.Value == i).First().Key)
                            {
                                case "Lopnr":
                                    try
                                    {
                                        r.RowID = Convert.ToInt32(RowFields[i]);
                                    }
                                    catch
                                    {
                                        r.RowID = 0;
                                    }
                                    break;
                                case "Sidnr":
                                    r.PageNo = RowFields[i];
                                    break;
                                case "Ordnr":
                                    r.OrdNo = RowFields[i];
                                    break;
                                case "Fdatum":
                                    r.BirthDate = RowFields[i];
                                    break;
                                case "Dpdatum":
                                    r.ChristeningDate = RowFields[i];
                                    break;
                                case "Kon":
                                    try
                                    {
                                        if (!string.IsNullOrWhiteSpace(RowFields[i]))
                                        {
                                            r.Gender = Convert.ToChar(RowFields[i]);
                                        }
                                        else
                                        {
                                            r.Gender = 'O';
                                        }
                                    }
                                    catch
                                    {
                                        r.Gender = 'O';
                                    }
                                    break;
                                case "Fnamn":
                                    r.FirstName = RowFields[i];
                                    break;
                                case "Enamn":
                                    r.SurName = RowFields[i];
                                    break;
                                case "Adress":
                                    r.Address = RowFields[i];
                                    break;
                                case "Nadress":
                                    r.NAddress = RowFields[i];
                                    break;
                                case "Mtitel":
                                    r.FatherTitle = RowFields[i];
                                    break;
                                case "Mfnamn":
                                    r.FatherFirstName = RowFields[i];
                                    break;
                                case "Menamn":
                                    r.FatherSurName = RowFields[i];
                                    break;
                                case "Ktitel":
                                    r.MotherTitle = RowFields[i];
                                    break;
                                case "Kfnamn":
                                    r.MotherFirstName = RowFields[i];
                                    break;
                                case "Kenamn":
                                    r.MotherSurName = RowFields[i];
                                    break;
                                case "Kalder":
                                    r.MotherAge = RowFields[i];
                                    break;
                                case "Ovr1":
                                    r.Extra1 = RowFields[i];
                                    break;
                                case "Ovr2":
                                    r.Extra2 = RowFields[i];
                                    break;
                                case "Ovr3":
                                    r.Extra3 = RowFields[i];
                                    break;
                                //case "Mtitel":
                                //    r.MTitle = RowFields[i];
                                //    break;
                                //case "Mfnamn":
                                //    r.MFirstName = RowFields[i];
                                //    break;
                                //case "Menamn":
                                //    r.MSurName = RowFields[i];
                                //    break;
                                //case "Ktitel":
                                //    r.KTitle = RowFields[i];
                                //    break;
                                //case "Kfnamn":
                                //    r.KFirstName = RowFields[i];
                                //    break;
                                //case "Kenamn":
                                //    r.KSurName = RowFields[i];
                                //    break;
                                case "Uppdaterat":
                                    r.Updated = RowFields[i];
                                    break;
                                default:
                                    break;
                            }
                        }
                        catch (Exception ex) { /*System.Windows.MessageBox.Show(ex.Message); */ }
                    }
                }
                if (r.IsChanged)
                {
                    r.IsChanged = false;
                    Records.Add(r);
                }
            }
        }



        public bool Write(string FileName, BirthBook book)
        {
            this.bk = book;
            this.FileName = FileName;
            return Write();
        }

        public bool Write()
        {
            if (FileName.ToUpper().EndsWith(".XML"))
            {
                return WriteXML();
            }
            else if (FileName.ToUpper().EndsWith(".TXT"))
            {
                return WriteTXT();
            }
            return false;
        }

        private bool WriteTXT()
        {
            return false;
        }

        private bool WriteXML()
        {
            try
            {
                //string LocationsFile = System.IO.Directory.GetCurrentDirectory() + "\\locations.dat";
                using (FileStream stream = new FileStream(FileName, FileMode.Create))
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<BirthBook>));
                        using (var writer = new StreamWriter(stream))
                        {
                            xs.Serialize(writer, bk);
                        }
                    }

            }
            catch { }
            return false;
        }


    }
}
